<template>
	<view>
		<Header :title="name" bgColor="#FFFFFF" :showBack="true"></Header>
		<view class="search">
			<input type="text" v-model="word" placeholder="搜索" />
		</view>

		<view class="box">
			<view v-if="item.ownerName?item.ownerName.includes(word):false" v-for="(item,index) in list" :key="index">
				<view @click="playAudio(item.ownerName+'准备采集')">{{item.ownerName}}</view>
				
				<view>
					<view @click="toSaolian(item)" v-if="item.isGather" style="background-color: #F3B92A; border: 1upx solid #F3B92A;">修改</view>
					<button @click="getTitle(item)" v-if="item.isGather&&!item.isSign" open-type="share" style="background-color: #07C160;">转发</button>
					<view @click="next(item.id)" v-if="!item.isGather&&item.isAgree" style="background-color: #ED3F14;border: 1upx solid #F3B92A;">跳过</view>
					<view @click="toSaolian(item,item.isGather,index+1)" :class="{'active':item.isGather}">{{item.isGather?'已采集':'去采集'}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var myPlugin = requirePlugin('WechatSI');
	var that
	export default {
		data() {
			return {
				innerAudioContext:uni.getBackgroundAudioManager(),
				baseUrl:getApp().globalData.baseUrl,
				word:'',
				list: [],
				id:'',
				name:'',
				title:'11111'
			}
		},
		onShow() {
				this.getStudentList()
		},
		onLoad(e) {
			that = this
			this.id = e.id
			this.name = e.name
		},
		async onShareAppMessage(res) {
			if (res.from === 'button') {// 来自页面内分享按钮
			
			await new Promise((suc,fal)=>{
				setTimeout(()=>{
					suc('')
				},0)
			})
			
				return {
				  title: this.title,
				  imageUrl: this.baseUrl + '/images/img/img6.jpg',
				  path: '/pages/index/index'
				}
			}
		    
		},
		methods: {
			getTitle(item){
				this.title = item.organizationName + '-' + item.gradeName + '-' + item.ownerName
			},
			getStudentList() {
				this.$http('/member/api/memberCard/getOperatorMemberCardList', {
					"condition": {
						"organizationId": "",
						"gradeId": this.id,
						 "dataTypeFlag":2,
						"isGatherQuery": true
					}
				}, async res => {
					
					let list1 = []
					let list2 = []
					let list3 = []
					
					for (let item of res.data.items) {
						if(item.isGather){
							list2.push(item)
						}else{
							if(item.isAgree){
								list1.push(item)
							}else{
								list3.push(item)
							}
						}
					}
					
					this.list = list1.concat(list2).concat(list3)
					for (let item of this.list) { 
					  item.ownerName = await this.decryptPhone(item.ownerName); 
					}
				})
			},
			decryptPhone(value) {
				if (value === null || value === undefined || value.trim() === '') {
				  return Promise.resolve('');
				}
				return new Promise((resolve,reject)=>{
					// if (value.length > 20 && /[0-9]/.test(value) && /[a-zA-Z]/.test(value)) {
					// if (/[0-9]/.test(value) && /[a-zA-Z]/.test(value)) {
						this.$http('/kernel/api/privacy/decryptPhone',{"CipherTexts":[value] }, res=>{
							if(res.data.data.length>0){
									resolve(res.data.data[0].phone) 
								}else{
									resolve(value)
								}
						})
					// }else{
						// resolve(value)
					// }
				})
			},
			async toSaolian(item,flag,play) {
					if(flag)return;
					if(play){
						let obj = play<this.list.length?this.list[play]:{isGather:true}
						let str = obj.isGather?item.ownerName+'开始采集':item.ownerName+'开始采集，'+obj.ownerName+'做准备'
						this.playAudio(str)
					}
					let signPhone = await this.decryptPhone(item.phone)
					this.$http('/pay/api/offlineFacePay/getToken', {
						"organization_id": item.wxOrganizationId,
						"out_user_id": item.id.replace(/-/g, "")
					}, res => {
						let schoolList = []
						schoolList.push(item.organizationName)
						wx.navigateToMiniProgram({
							appId: 'wx5931af4836330935', //采集小程序appid
							path: '/pages/face_take_photos/take_photos?is_first=1', //小程序路径
							extraData: {
								type: 'take_photo', //采集跳转
								user_name: item.ownerName, //刷脸用户姓名
								school_name: item.organizationName, //学校
								cert_info: {
									'cert_type': 'IDCARD',
									'cert_id': '123',
									'name': item.ownerName
								}, //cert_type:IDCARD（身份证）,EEP_HK_MACAU（港澳通行证）,PASSPORT_NO（护照）；cert_id:证件号;name:姓名
								user_type: 'STUDENT', //职业(STUDENT：学生,STAFF：职员)
								student_info: {
									'class_name': item.gradeName
								}, //class_name:班级。student_info和staff_info二选一
								// staff_info:{'occupation':'教师'}, //occupation:职业 student_info和staff_info二选一
								school_list: schoolList, //学校列表
								organization_id: item.wxOrganizationId, //organazition_id
								out_user_id: item.id.replace(/-/g, ""), //out_user_id
								account: signPhone, //签约手机号
								token: res.data.token
							},
							// envVersion: 'trial',    // 如果要跳转体验版，加上这个参数，否则请不要加这个参数
							success(res1) {
								// 成功跳转到签约小程序 
								uni.setStorageSync('id',item.id)
							}
						})

					})
			},

			playAudio(e){
				myPlugin.textToSpeech({   // 调用插件的方法
				  lang: 'zh_CN',
				  content:e,
				  success: function (res) {
						that.innerAudioContext.title = res.filename
						that.innerAudioContext.src = res.filename
						that.innerAudioContext.play();
				  },
				})
			},
			next(id){
				uni.showModal({
				  title: '温馨提示',
				  content: '确认跳过？',
				  success: function (res) {
				    if (res.confirm) {
							let list=[]
							list.push(id)
							that.$http('/minprogram/api/member/unAgree',list,res=>{
								that.getStudentList()
							})
				    } else if (res.cancel) {}
				  }
				});
			}

		},
	}
</script>

<style scoped lang="less">
	.search {
		height: 100upx;
		box-sizing: border-box;
		padding-top: 20upx;
		>input{
			width: 93%;
			height: 60upx;
			margin: 0 auto;
			border-radius: 8upx;
			background-color: #FFFFFF;
			text-align: center;
			font-size: 26upx;
		}
	}

	.box {
		width: 93%;
		margin: auto;
		background-color: #FFFFFF;
		>view {
			width: 100%;
			height: 150upx;
			border-bottom: 1upx solid #dddddd;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			padding: 0 40upx;
			position: relative;
			>view:first-child {
				font-size: 40upx;
				height: 100%;
				line-height: 150upx;
			}
			
			>view:last-child{
				display: flex;
				>view,button {
					background-color: #07C160;
					border: 1upx solid #07C160;
					color: #FFFFFF;
					font-size: 24upx;
					width: 100upx;
					height: 52upx;
					border-radius: 8upx;
					line-height: 52upx;
					text-align: center;
					margin-left: 20upx;
					padding: 0;
					box-sizing: border-box;
				}
				>view:last-child.active {
					color: #07C160;
					background-color: #FFFFFF;
				}
			}
			
			
		}
	}
</style>


